// pages/partner/storycircle/storycircle.js
var shihan = require('../../../common/fetch')
var config = require('../../../common/config')
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    page_size: 20,
    listflag: true,
    has_more: true,
    imgflag: true,
    showLoading: true,
    material_list: [],
    story_cache: {},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {//material_list
    this.getMaterialList()
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    wx.removeStorageSync('canvasdrawer_pic_story_cache');
    this.story_cache = wx.getStorageSync('canvasdrawer_pic_story_cache') || {};
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  getMaterialList: function () {
    var that = this;
    shihan.sendRequest(config.material_list, 'POST', { page: that.data.page, page_size: that.data.page_size })
      .then(function (response) {
        if (response.data.code == 0) {
          if (that.data.page == 1) {
            that.setData({
              material_banner: response.data.data.sh_banner
            })
          }
          if (response.data.data.sh_items.length>0) {
            that.setData({
              listflag: true,
              has_more: true,
              page: that.data.page + 1,
              material_list: that.data.material_list.concat(response.data.data.sh_items)
            })
            if (that.data.material_list.length == response.data.data.sh_total_rows) {
              that.setData({
                listflag: false,
                has_more: false
              })
            } 
          } else {
            that.setData({
              listflag: false,
              has_more: false
            })
          }
        } else {
          that.setData({
            listflag: false,
            has_more: false
          })
        }
        that.setData({
          showLoading: false
        })
        wx.stopPullDownRefresh()
      }, function (error) {
        console.log(error);
      });
  },

  jumpadvert: function (e) { //广告位跳转
    app.AdSkipper(e)
  },

  previewImg: function (e) {
    var that = this, commentidx = e.currentTarget.dataset.commentidx,
      img_type = e.currentTarget.dataset.type,
      imgidx = e.currentTarget.dataset.imgidx,
      imgs = this.data.material_list[commentidx].sh_images,
      imgs_qrcode = this.data.material_list[commentidx].sh_qr_code;
    
    if (img_type == 1) {
      that.setData({
        painting: {
          width: 560,
          height: 900,
          clear: true,
          views: [
            {
              type: 'image',
              url: imgs[imgidx],
              top: 0,
              left: 0,
              width: 560,
              height: 900
            },
            {
              type: 'image',
              url: imgs_qrcode || 'https://img-oss.shihanbainian.com/shihan2/index/defaults.png',
              top: 593,
              left: 183,
              width: 200,
              height: 200,
              borderRadius: 100
            }
          ]
        },
        imgflag: false
      })
    } else {
      wx.previewImage({
        current: imgs[imgidx],
        urls: imgs
      })
    }
  },
  eventGetImage: function (event) {
    // console.log(event)
    const { tempFilePath, errMsg } = event.detail;
    if (errMsg === 'canvasdrawer:ok') {
      this.setData({
        shareImage: tempFilePath
      })
    }
  },

  closeimg: function () {
    this.setData({
      imgflag: true
    })
  },
  downloadimg: function () {
    shihan.saveImage(this.data.shareImage); //保存图片
  },

  reproduce: function (e) {//一键复制
    var that = this, index = e.currentTarget.dataset.index,
      img_type = e.currentTarget.dataset.type,
      word = String(e.currentTarget.dataset.word),
      imgs = this.data.material_list[index].sh_images;
    wx.setClipboardData({
      data: word,
      success: function (res) {
        wx.getClipboardData({
          success: function (res) {
            if (res.data) {
              // shihan.showToast.call(that, '复制成功');
            }
          }
        })
      }
    })
    // if (img_type == 1) {
    //   shihan.saveImage(that.data.shareImage); //保存图片
    // } else {
    //   imgs.forEach(function (el, index){
    //     that.getImageInfo(el).then(file => {
    //       shihan.saveImage(file); //保存图片
    //     })
    //   })
    // }
  },

  getImageInfo: function (url) {
    return new Promise((resolve, reject) => {
      if (this.story_cache[url]) {
        resolve(this.story_cache[url])
      } else {
        const objExp = new RegExp(/^http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?/)
        if (objExp.test(url)) {
          wx.getImageInfo({
            src: url,
            complete: res => {
              if (res.errMsg === 'getImageInfo:ok') {
                this.story_cache[url] = res.path
                resolve(res.path)
              } else {
                reject(new Error('getImageInfo fail'))
              }
            }
          })
        } else {
          this.story_cache[url] = url
          resolve(url)
        }
      }
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      page: 1,
      page_size: 20,
      listflag: true,
      showLoading: true,
      material_list: []
    })
    this.getMaterialList()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.listflag) {
      this.setData({
        listflag: false
      })
      this.getMaterialList()
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  }
})